package com.jiuyin.dianjing.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 保存Preferences
 */
public class SPFUtil {
    private static final String SP_FILE_NAME = "config";
    private static SharedPreferences mPreferences;
    private static SharedPreferences.Editor mEditor;
    private static SPFUtil mSPFUtil;

    // 构造方法
    private SPFUtil(Context context) {
        mPreferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        mEditor = mPreferences.edit();
    }

    // 单例模式
    public static SPFUtil getInstance(Context context) {
        if (mSPFUtil == null) {
            synchronized (SPFUtil.class) {
                if (null == mSPFUtil) {
                    mSPFUtil = new SPFUtil(context);
                }
            }
        }
        return mSPFUtil;
    }

    // 存入数据
    public void putSP(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }

    public void putSPBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    // 获取数据
    public String getSP(String key) {
        return mPreferences.getString(key, "");
    }

    public int getSpInt(String key) {
        return mPreferences.getInt(key, 0);
    }

    public void putSpInt(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }

    public boolean getSPBoolean(String key) {
        return mPreferences.getBoolean(key, false);
    }

    public boolean getSPBoolean(String key, boolean defValue) {
        return mPreferences.getBoolean(key, defValue);
    }

    // 移除数据
    public void removeSP(String key) {
        mEditor.remove(key);
        mEditor.commit();
    }
}
